package com.easyexcle.demo.easyExcel.conver;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Author: lkz
 * @Title: SexConverter
 * @Description: 性别转换器
 * @Date: 2022/12/6 14:31
 */

public class SexConverter implements Converter<String> {

    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        if("男".equals(cellData.getStringValue())){
            return "1";
        }else if("女".equals(cellData.getStringValue())){
            return "2";
        }else{
            return "0";
        }
    }


    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        CellData<String> data = new CellData<>();
        if(value.equals(1)){
           data.setStringValue("男");
       }else if(value.equals(2)){
            data.setStringValue("女");
        }else{
            data.setStringValue("未知");
        }
        return data;

    }
}
